package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.dao.User;
import cn.com.dashihui.web.service.UserMoneyService;
import cn.com.dashihui.web.service.UserService;

@RequiresAuthentication
public class UserMoneyController extends BaseController{
	private UserMoneyService service = new UserMoneyService();
    private UserService userService = new UserService();
	/**
	 * 查询分公司列表
	 */
    public void index(){
    	List<Store> storeList=service.findStoreList();
		setAttr("storeList", storeList);
        render("index.jsp");
    }
  //余额明细列表
	public void page(){
		int pageNum = getParaToInt(0, 1);
		String phone=getPara("phone");//手机号
		String name=getPara("name");//手机号
		String beginDate=getPara("beginDate");//时间
		String endDate=getPara("endDate");
		String stores=getPara("stores");//所属分公司
		String types=getPara("types");//类别
		int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
		String storeIds="";
		if(StrKit.notBlank(stores)){
			 List<Store> st=userService.findStoreByIds(stores);
			 for (int i = 0; i < st.size(); i++) {
				if(i==0){
					storeIds=st.get(i).get("id")+"";
				}else{
					storeIds+="','"+st.get(i).get("id");
				}
			}
		 }
		logger.info(">>>>>>>>>>>>>"+storeIds);
		renderResult(0,service.findByPage(pageNum, pageSize,phone,name,beginDate,endDate,types,storeIds));
/*		renderSuccess(MapKit.newInstance()
				.put("money", money)
				.put("remarkid", remarkid)
				.getAttrs());*/
	}
	/**
	 * 余额综合
	 */
     public void findMoney(){
	 		String phone=getPara("phone");//手机号
	 		String name=getPara("name");//手机号
	 		String beginDate=getPara("beginDate");//时间
	 		String endDate=getPara("endDate");
	 		String stores=getPara("stores");//所属分公司
	 		String types=getPara("types");//类别
	 		String title="";
			if(StrKit.notBlank(stores)){
				 List<Store> st=userService.findStoreByIds(stores);
				 for (int i = 0; i < st.size(); i++) {
					if(i==0){
						title=st.get(i).get("title");
					}else{
						title+="','"+st.get(i).get("title");
					}
				}
			 }
			double money, money1, nowMoney;

			try {
				Record userMoney;
				Record userMoney1;

				if (StrKit.notBlank(types)) {
					// 查询充值或退款的金额
					if (types.equals("1") || types.equals("3")) {
						userMoney = service.findByMoney(phone, name, beginDate, endDate, types, title);
						nowMoney = userMoney.get("money");
						System.out.println("充值货退款====="+nowMoney);
					} else {
						// 查询消费的金额
						userMoney1 = service.findByMoney1(phone, name, beginDate, endDate, types, title);
						nowMoney = userMoney1.get("money");
						System.out.println("消费===="+nowMoney);
					}
					renderSuccess("nowMoney", nowMoney);
				} else {
					userMoney = service.findByMoney(phone, name, beginDate, endDate, types, title);
					// 查询减少的金额
					userMoney1 = service.findByMoney1(phone, name, beginDate, endDate, types, title);

					System.out.println("userMoney.money的1值是" + userMoney.get("money"));
					System.out.println("userMoney1.money的1值是" + userMoney1.get("money"));

					money = userMoney.get("money");// 增加的
					money1 = userMoney1.get("money");// 减少的的
					nowMoney = money - money1; // nowMoney;总金额
				}

			} catch (Exception e) {
				nowMoney = 0.00;
			}
			System.out.println("nowMoney的值是" + nowMoney+"因为无入金总额，所以金额总和更改为消费");
			renderSuccess("nowMoney", nowMoney);
     }
     /**
 	 * 会员详情
 	 */
 	public void detail(){
 		int id = getParaToInt(0,0);
 		if(id!=0){
 		User user=userService.findById(id);
 			setAttr("user", user);
 			List<Store> storeList=userService.findStoreList();
 			setAttr("storeList", storeList);
 			List<Record> userList=userService.findUserList(user.get("store"));
 			setAttr("userList", userList);
 		}
 		render("detail.jsp");
 	}
 	/**
	 * 选择营业部
	 */
	public void chooseDao(){
		String[] store = getParaValues("store");
		String str = java.lang.String.join("','", store);
		renderSuccess(userService.chooseDao(str));
	}
	/**
	 * 编辑会员
	 */
	public void doEdit(){
		int userid=getParaToInt("userid",0);
		
		String sppoint=getPara("sppoint");
		String jypoint=getPara("jypoint");
		String jkpoint=getPara("jkpoint");
		
		String phone=getPara("phone");
		String store=getPara("store");
		String toBossOneId=getPara("toBossOneId");
		if(userid !=0){
			User user=userService.findById(userid);
			if(StrKit.notNull(user)){
				if(StrKit.notBlank(phone)){
					if(!user.get("phone").equals(phone)){
						User users=userService.findByPhone(phone);
						if(StrKit.notNull(users)){
							renderResult(2);
							return;
						}else{
							user.set("phone", phone);
						}
					}
				}
				if(StrKit.notBlank(jypoint)){
					user.set("jypoint", jypoint);
				}
				if(StrKit.notBlank(sppoint)){
					user.set("sppoint", sppoint);
				}
				if(StrKit.notBlank(jkpoint)){
					user.set("jkpoint", jkpoint);
				}
				if(StrKit.notBlank(store)){
					 Store st=userService.findStoreById(store);
					 user.set("store", st.get("title"));
				}
				if(StrKit.notBlank(store)&&StrKit.notBlank(toBossOneId)){
					user.set("toBossOneId", toBossOneId);
				}else if(StrKit.notBlank(store)&&StrKit.isBlank(toBossOneId)){
					user.set("toBossOneId", "");
				}
				 if(user.update()){
					 userService.findById(userid);
					 renderSuccess();
					 return;
				 }else{
					renderFailed();
						return;
				 }
			}else{
				renderFailed();
				return;
			}
		}else{
			renderFailed();
			return;
		}
	}
	
     
	/**
	 * excel导出
	 */
	public void doVipExcel(){
		List<Pair> titles = new ArrayList<Pair>();
	    titles.add(new Pair("name", "会员姓名"));
	    titles.add(new Pair("phone", "手机号码"));
	    titles.add(new Pair("amount", "金额"));
	    titles.add(new Pair("createDate", "时间"));
	    titles.add(new Pair("type", "类别"));
	    titles.add(new Pair("store", "所属分公司"));

	    String name=getPara("name");
	    String phone=getPara("phone");
	    String beginDate=getPara("beginDate");
	    String endDate=getPara("endDate");
		String types=getPara("types");
		String stores=getPara("stores");
		String title="";
		if(StrKit.notBlank(stores)){
			 List<Store> st=userService.findStoreByIds(stores);
			 for (int i = 0; i < st.size(); i++) {
				if(i==0){
					title=st.get(i).get("title");
				}else{
					title+="','"+st.get(i).get("title");
				}
			}
		 }
		List<Record> list= service.findExcelVipByPage(phone,name,beginDate,endDate,types,title);

		List<Record> listz=new ArrayList<Record>();
		for (int i = 0; i < list.size(); i++) {
			Record record=new Record();
				record.set("name", list.get(i).get("name"));
				record.set("phone", list.get(i).get("phone"));
				//金额增加与减少
				if (list.get(i).get("type").equals(1)) {
					record.set("amount", "+"+list.get(i).get("amount"));
				} else if (list.get(i).get("type").equals(2)) {
					record.set("amount", "-"+list.get(i).get("amount"));
				}

				record.set("createDate", list.get(i).get("createDate"));
				// 类别
				if (list.get(i).get("orderType").equals(1)) {
					if (list.get(i).get("type").equals(1)) {
						record.set("type", "退款");
					} else if (list.get(i).get("type").equals(2)) {
						record.set("type", "消费");
					}
				} else if (list.get(i).get("orderType").equals(2)) {
					record.set("type", "充值");
				}

				record.set("store", list.get(i).get("store"));
			listz.add(record);
 		}
		// 导出
		service.exportVip(getResponse(), getRequest(),titles, listz);		
		renderNull();
	}
	
}